#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QtMath>
#include <QTimer>
#include <QDebug>

int flag=0;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    resize(1600,900);
    setStyleSheet("background-color:balck");//黑色背景
//    QTimer *time1=new QTimer(this);
//    time1->start(20);
//    connect(time1,&QTimer::timeout,this,&Widget::timer_timeout);
    serial=new QSerialPort(this);

    QString PortString;//创建容器

    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())//foreach关键字，遍历列表，放到info中
    {
        PortString=info.portName();//将可用串口的名字放到字符串容器中

        qDebug()<<PortString;
    }

    serial->setPortName(PortString);
    serial->setBaudRate(9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);//没有检验
    serial->setStopBits(QSerialPort::OneStop);

    serial->setFlowControl(QSerialPort::NoFlowControl);
    connect(serial,QSerialPort::readyRead,this,&Widget::serial_port);//当串口有数据时，转到槽
    serial->open(QIODevice::ReadWrite);//打开串口
}

Widget::~Widget()
{
    delete ui;
}
void Widget::paintEvent(QPaintEvent *)
{
//    QPainter paint(this);
//    QPicture img;
//    img.load("E://cc.fff");
//    paint.drawPicture(0,0,img);//加载图片

    QPainter paint(this);
    paint.setRenderHint(QPainter::Antialiasing);
    QPen pen;
    pen.setColor(Qt::green);
    paint.setPen(pen);

    paint.drawArc(100,100,1400,1400,0*16,180*16);//左上角地址，右下角地址，起始角度和结束角度
    paint.drawArc(240,240,1120,1120,0*16,180*16);//左上角地址，右下角地址，起始角度和结束角度
    paint.drawArc(380,380,840,840,0*16,180*16);//左上角地址，右下角地址，起始角度和结束角度
    paint.drawArc(520,520,560,560,0*16,180*16);//左上角地址，右下角地址，起始角度和结束角度
    paint.drawArc(660,660,280,280,0*16,180*16);//左上角地址，右下角地址，起始角度和结束角度

    paint.drawLine(0,800,1600,800);//左上角是0,0点

    paint.save();
    paint.translate(800,800);
    for(int i=0; i<5;i++)
    {
        paint.rotate(-30);//旋转30度

        paint.drawLine(0,0,720,0);
    }
    paint.restore();//返回保存坐标

    QFont font("仿宋",30,QFont::Bold,true);
    paint.setFont(font);

    paint.save();
    paint.translate(800,800);
    /*************显示角度信息*************/
    for(int i=0;i<7;i++)
    {
        int r=i*30;

        QString t= QString::number(r)+"°";

        paint.drawText(720*qCos(qDegreesToRadians(30.0*i)),-720*qSin(qDegreesToRadians(30.0*i)),t);
    }

    /*************显示距离*************/
    for(int i=1;i<6;i++)
    {
        int temp_distance=i*10;
        QString distance=QString::number(temp_distance)+"cm";
        paint.drawText(140*i,30,distance);
    }


    paint.restore();


    pen.setWidth(15);
    paint.setPen(pen);
    paint.save();
    paint.translate(800,800);


//    paint.drawLine(0,0,700*qCos(qDegreesToRadians(angle)),-700*qSin(qDegreesToRadians(angle)));//绘制扫描线

/****************用渐变线实现绘制辉光余晖效果****************/
    double temp_angle=angle;
    if(flag==0)
    {
        if(distance>=50.0)
        {
            pen.setColor(Qt::green);
            paint.setPen(pen);
            int x=700*qCos(qDegreesToRadians(temp_angle));
            int y=-700*qSin(qDegreesToRadians(temp_angle));
            paint.drawLine(0,0,x,y);//绘制扫描线
        }
        else
        {
            pen.setColor(Qt::green);
            paint.setPen(pen);
            int x=distance*14*qCos(qDegreesToRadians(temp_angle));
            int y=-distance*14*qSin(qDegreesToRadians(temp_angle));
            paint.drawLine(0,0,x,y);//绘制扫描线

            pen.setColor(Qt::red);
            paint.setPen(pen);
            int x1=700*qCos(qDegreesToRadians(temp_angle));
            int y1=-700*qSin(qDegreesToRadians(temp_angle));
            paint.drawLine(x,y,x1,y1);//绘制扫描线
        }
        for(int i=1;i<=temp_angle;i++)
        {
            paint.setOpacity(1-i*1.0/((temp_angle>30)?30:temp_angle));
            if(arr[int(temp_angle-i)]>=50.0)
            {
                pen.setColor(Qt::green);
                paint.setPen(pen);
                int x=700*qCos(qDegreesToRadians(temp_angle-i));
                int y=-700*qSin(qDegreesToRadians(temp_angle-i));
                paint.drawLine(0,0,x,y);//绘制扫描线
            }
            else
            {
                pen.setColor(Qt::green);
                paint.setPen(pen);
                int x=arr[int(temp_angle-i)]*14*qCos(qDegreesToRadians(temp_angle-i));
                int y=-arr[int(temp_angle-i)]*14*qSin(qDegreesToRadians(temp_angle-i));
                paint.drawLine(0,0,x,y);//绘制扫描线\

                pen.setColor(Qt::red);
                paint.setPen(pen);
                int x1=700*qCos(qDegreesToRadians(temp_angle-i));
                int y1=-700*qSin(qDegreesToRadians(temp_angle-i));
                paint.drawLine(x,y,x1,y1);//绘制扫描线
                if(i==30)
                {
                    break;
                }
            }

        }

    }



    else if(flag==1)
    {
        if(distance>=50.0)
        {
            pen.setColor(Qt::green);
            paint.setPen(pen);
            int x=700*qCos(qDegreesToRadians(temp_angle));
            int y=-700*qSin(qDegreesToRadians(temp_angle));
            paint.drawLine(0,0,x,y);//绘制扫描线
        }
        else
        {
            pen.setColor(Qt::green);
            paint.setPen(pen);
            int x=distance*14*qCos(qDegreesToRadians(temp_angle));
            int y=-distance*14*qSin(qDegreesToRadians(temp_angle));
            paint.drawLine(0,0,x,y);//绘制扫描线

            pen.setColor(Qt::red);
            paint.setPen(pen);
            int x1=700*qCos(qDegreesToRadians(temp_angle));
            int y1=-700*qSin(qDegreesToRadians(temp_angle));
            paint.drawLine(x,y,x1,y1);//绘制扫描线
        }
        for(int i=1;i<=(180-temp_angle);i++)
        {
            paint.setOpacity(1-i*1.0/(((180-temp_angle)>30)?30:(180-temp_angle)));
            if(arr[int(temp_angle+i)]>=50.0)
            {
                pen.setColor(Qt::green);
                paint.setPen(pen);
                int x=700*qCos(qDegreesToRadians(temp_angle+i));
                int y=-700*qSin(qDegreesToRadians(temp_angle+i));
                paint.drawLine(0,0,x,y);//绘制扫描线
            }
            else
            {
                pen.setColor(Qt::green);
                paint.setPen(pen);
                int x=arr[int(temp_angle+i)]*14*qCos(qDegreesToRadians(temp_angle+i));
                int y=-arr[int(temp_angle+i)]*14*qSin(qDegreesToRadians(temp_angle+i));
                paint.drawLine(0,0,x,y);//绘制扫描线\

                pen.setColor(Qt::red);
                paint.setPen(pen);
                int x1=700*qCos(qDegreesToRadians(temp_angle+i));
                int y1=-700*qSin(qDegreesToRadians(temp_angle+i));
                paint.drawLine(x,y,x1,y1);//绘制扫描线
                if(i==30)
                {
                    break;
                }
            }

        }
//            paint.drawLine(0,0,700*qCos(qDegreesToRadians(temp_angle+i)),-700*qSin(qDegreesToRadians(temp_angle+i)));
//            if(i==30)
//            {
//                break;
//            }
//        }
    }
    paint.restore();
}


//void Widget::timer_timeout()
//{
//    //qDebug()<<"ddd";
//    //static i=0;
//    //i++;
//    if(flag==0)
//    {
//        angle++;
//        if(angle==180)
//        {
//            flag=1;
//        }

//    }
//    else
//    {
//        angle--;
//        if(angle==0)
//        {
//            flag=0;
//        }
//    }

//    update();//重新绘制
//}

void Widget::serial_port()
{

    QString angle_data,angle_str,distance_str;
    rx_data=serial->readAll();//从串口接收数据
    rx_len=rx_data.length();//调用qstring中的方法
    rx_data=rx_data.toLatin1();//转为ascii字符串的方法

    //qDebug()<<rx_data;

    if(rx_data[0]=='+')
    {
        flag=0;
    }
    else
    {
        flag=1;
    }

    if(rx_data[0] == '+')
        flag = 0;
    else
        flag = 1;
    for(int i = 1; i < rx_len - 1; i++)
        angle_data += rx_data[i];

    int index = angle_data.indexOf(' ');
    for(int i = 0; i < index; i++)
        angle_str += angle_data[i];
    angle = angle_str.toInt();

    for(int i = index + 1; i < sizeof(angle_data); i++)
        distance_str += angle_data[i];
    distance =distance_str.toInt();

    arr[int(angle)] = distance;

    qDebug() << angle << " " << distance;

    update();
}
